package cn.xawl.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author lh
 * @date 2020/06/22 14:35:33
 **/
public class StatusConstraintValidator implements ConstraintValidator<StatusValue, Integer> {
    private Set<Integer> set = new HashSet<>();
    /**
     * 初始化
     */
    @Override
    public void initialize(StatusValue constraintAnnotation) {
        int[] values = constraintAnnotation.values(); //获取自定义的状态信息 0，1
        for (int value : values) {
            set.add(value);
        }
    }

    /**
     * 判断是否校验成功
     * @param value 要判断具体值
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value); //判断具体的值是否含有0或1
    }
}
